class Subject{
    constructor(){
        this.state = 0;
        this.observers = []
    }
    getState(){
        return this.state
    }
    setState(state){
        this.state = state
        this.notifyAllObservers()
    }
    attch(observer){
        this.observers.push(observer)
    }
    notifyAllObservers(){
        this.observers.forEach(observer=>{
            observer.update()
        })
    }
}

class Observer{
    constructor(name,subject){
        this.name = name
        this.subject = subject
        this.subject.attch(this)
    }
     update(){
         console.log(`${this.name} update, state ${this.subject.getState()}`)
     }
}

var s = new Subject()

var o1 = new Observer('01',s)
var o2 = new Observer('02',s)
var o3 = new Observer('03',s)

s.setState(1)
s.setState(2)
s.setState(3)